const app = getApp()
let qCloud = require("../../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    warehouseId: null,
    warehouseName: "",
    warehouseInfo: {
      warehouseId: null,
      warehouseCode: null,
      warehouseName: "",
      warehouseTotal: null,
      warehouseType: null,
      warehouseStatus: null,
      deletedMark: false,
      subWarehouseList: []
    },
    subWarehouseList: []
  },
  // 仓库
  warehouseNameInput(e) {
    let self = this;
    let warehouseName = e.detail.value;
    self.data.warehouseInfo["warehouseName"] = warehouseName;
    self.setData({
      warehouseName: warehouseName,
      warehouseInfo: self.data.warehouseInfo
    });
  },
  // 子仓库
  subWarehouseNameInput(e) {
    let self = this;
    let index = e.currentTarget.dataset.index;
    self.data.subWarehouseList[index].warehouseName = e.detail.value;
    self.setData({
      subWarehouseList: self.data.subWarehouseList
    });
  },

  // 添加仓库
  addTap(e) {
    let self = this;
    let subWarehouse = {
      warehouseId: null,
      warehouseCode: null,
      warehouseName: "",
      warehouseTotal: null,
      warehouseType: "3",
      warehouseStatus: "1",
      deletedMark: false,
      subWarehouseList: []
    };
    let subWarehouseList = self.data.subWarehouseList;
    subWarehouseList.push(subWarehouse);
    self.setData({
      subWarehouseList: subWarehouseList
    });
  },
  // 删除仓库
  delTap(e) {
    let self = this;
    let index = e.currentTarget.dataset.index;
    let subWarehouseList = self.data.subWarehouseList;

    if(subWarehouseList[index].warehouseId){
      // 工序ID不为空，则代表为修改项，那么就修改为被删除
      subWarehouseList[index].deletedMark = true;
    }else{
      // 工序ID为空，则代表为新增项，那么就直接移除
      subWarehouseList.splice(index, 1);
    }

    self.setData({
      subWarehouseList: subWarehouseList
    });
  },

  /** 提交仓库 */
  submitWarehouse(e) {
    let self = this;

    let warehouseName = self.data.warehouseName;
    if (!warehouseName) {
      wx.showToast({
        title: "仓库名称不能为空",
        icon: "error"
      });
      return;
    }

    let subWarehouseList = self.data.subWarehouseList;
    let newSubWarehouseList = [];
    for (let i = 0; i < subWarehouseList.length; i++) {
      let subWarehouse = subWarehouseList[i];
      if (subWarehouse.warehouseName != "") {
        newSubWarehouseList.push(subWarehouse);
      }
    }
    self.data.warehouseInfo["subWarehouseList"] = newSubWarehouseList;

    let params = {
      warehouseInfo: self.data.warehouseInfo
    }
    app.fetchApis(self, qCloud.api.common_submitWarehouseInfo, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        wx.navigateBack({
          delta: 1
        });
      }
    });
  },
  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    let self = this;

    if (options.warehouseId) {
      self.setData({
        warehouseId: options.warehouseId
      });
      self.queryWarehouseDetails();
    }
  },
  /** 查询仓库详情 */
  queryWarehouseDetails: function (e) {
    let self = this;

    let params = {
      warehouseId: self.data.warehouseId
    };
    app.fetchApis(self, qCloud.api.common_queryWarehouseList, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        let warehouseInfo = result.data.warehouseList[0];

        self.setData({
          warehouseName: warehouseInfo.warehouseName,
          warehouseInfo: warehouseInfo,
          subWarehouseList: warehouseInfo.subWarehouseList
        });
      }
    });
  },
  /** 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {},
  /** 生命周期函数--监听页面显示 */
  onShow: function () {},
  /** 生命周期函数--监听页面隐藏 */
  onHide: function () {},
  /** 生命周期函数--监听页面卸载 */
  onUnload: function () {},
  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {},
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {},
  /** 用户点击右上角分享 */
  onShareAppMessage: function () {}
});
